<template>
	<view class="container">
		<u--form :model="form" :rules="rules" ref="uForm" labelWidth="90">
			<u-form-item label="来源名称" prop="name" borderBottom ref="item1">
				<u--input v-model="form.name"></u--input>
			</u-form-item>
		</u--form>
		<u-button type="success" @click="submit" color="#19BC9C" :customStyle="btnStyle">提交</u-button>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				rules: {
					'name': {
						type: 'string',
						required: true,
						message: '请填写来源名称',
						trigger: ['blur', 'change']
					},
				},
				form: {
					name: '',
				},
				btnStyle: {
					width: '100%',
					marginTop: '10px'
				},
			}
		},
		onReady() {
			this.$u.auth.check();
			//onReady 为uni-app支持的生命周期之一
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			submit() {
				this.$refs.uForm.validate().then(async () => {
					let result = await this.$u.api.manage.sourceAdd({
						name: this.form.name
					})
					if (result.code == 1) {
						this.$refs.uToast.show({
							type: 'success',
							message: result.msg,
							complete: function() {
								uni.navigateTo({
									url: '/pages/manage/source/index'
								});
							}
						})
					} else {
						this.$refs.uToast.show({
							type: 'error',
							message: result.msg
						})
					}
				}).catch(errors => {})
			}
		},
	}
</script>

<style>
	.container {
		padding: 8px;
	}
</style>